package com.jing.yao.config;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.web.servlet.LocaleResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;

/**
 * 国际化解析器
 * @author Yao
 * @since 2020-03-24
 */
public class MessageLocaleResolver implements LocaleResolver {

    private String defaultLocale = "zh-CN";

    private String localeHeader = "locale";

    @Autowired
    private MessageSource messageSource;

    @Override
    public Locale resolveLocale(HttpServletRequest request) {
        String language = request.getHeader(localeHeader);
        //当为空时,采用默认的
        if(StringUtils.isBlank(language)) {
            language = defaultLocale;
        }
        Locale locale = Locale.forLanguageTag(language);
        return locale;
    }

    @Override
    public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {

    }

}
